package erfgame.frontend.desktop.view.terrain.iso.handler;

import java.awt.event.MouseEvent;
import java.util.List;

import erfgame.core.Point3D;
import erfgame.core.entity.Entity;
import erfgame.core.graphics.renderer.chain.RendererChain;
import erfgame.core.world.terrain.insertion.SurfaceInsertion;

import erfgame.frontend.desktop.view.terrain.iso.EntitySelection;
import erfgame.frontend.desktop.view.terrain.iso.IsometricTerrainView;
import erfgame.frontend.desktop.view.terrain.iso.interaction.EntityInteractionDescriptionFactory;
import erfgame.frontend.desktop.view.terrain.iso.interaction.EntityInteractionDescriptionFactoryManager;
import erfgame.frontend.desktop.view.terrain.iso.interaction.EntityInteractionFactory;
import erfgame.frontend.desktop.view.terrain.iso.interaction.Interaction;
import erfgame.frontend.desktop.view.terrain.iso.interaction.InteractionDescription;

public class PrimaryInteractionHandler extends BaseInteractionHandler {

	private IsometricTerrainView view;
	private EntityInteractionDescriptionFactoryManager factoryManager;
	private EntitySelection selection;
	
	public PrimaryInteractionHandler( IsometricTerrainView view, EntitySelection selection, EntityInteractionDescriptionFactoryManager factoryManager ) {
		this.view = view;
		this.factoryManager = factoryManager;
		this.selection = selection;
	}

	@SuppressWarnings(value="unchecked")
	public MouseEvent[] handleMouseEvent(MouseEvent e) {
		if( e.getID() == MouseEvent.MOUSE_PRESSED && e.getButton() == MouseEvent.BUTTON1 ) {
			Point3D p = view.getTerrainPosition(e.getX(), e.getY(), new Point3D() );
			Entity selected = null;
			List<RendererChain> entities = selection.getSelected();
			if( entities != null ) {
				for( int i=0; i<entities.size(); i++ ) {
					RendererChain rendererChain;
					rendererChain = entities.get( i );
					Entity entity = rendererChain.getHead().getEntity();
					if( entity != null ) {
						selected = entity;
						break;
					}
				}
			}
			Entity clicked;
			List<SurfaceInsertion> insertions = view.getInsertionsAt(e.getX(), e.getY());
			if( insertions != null && insertions.size() > 0 ) {
				clicked = insertions.get(0).getRendererChain().getHead().getEntity();
			} else {
				clicked = null;
			}
			EntityInteractionDescriptionFactory factory = factoryManager.getFactory(selected);
			List<InteractionDescription> interactions = factory.getInteractions(clicked, p);
			if( interactions != null && interactions.size()>0 ) {
				InteractionDescription interactionDescription = interactions.get(0);
				EntityInteractionFactory interactionFactory;
				interactionFactory = interactionDescription.getInteractionFactory();
				if( interactionFactory != null ) {
					Interaction interaction = interactionFactory.createInteraction(clicked, p);
					if( interaction != null ) {
						InteractionHandler handler = interaction.activate();
						if( handler != null ) {
							view.addInteractionHandler( handler );
						}
					}
				}
			}
			return null;
		} else {
			return super.handleMouseEvent(e);
		}
	}
}
